package com.ch.mafia.service;

import com.ch.mafia.base.BaseDao;
import com.ch.mafia.base.BaseServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

/**
 * Servlet implementation class LoginService
 */
public class LoginService extends BaseServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginService() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doGet(request, response);
		JSONObject jsonParams = new JSONObject();
		JSONObject jsonResult = new JSONObject();
		try{
			if(request.getParameter("json")!=null){
				jsonParams=new JSONObject(request.getParameter("json"));
				System.out.println("jsonParams:"+jsonParams.toString());
			}
			String userName = jsonParams.getString("username");
			String password = jsonParams.getString("password");
			BaseDao baseDao =new BaseDao();
			JSONObject jsonObject = baseDao.queryBean("s_user", null, "username = ? and password=?", new String[]{userName,password});
			if(jsonObject!=null){
				jsonResult = jsonObject;
				jsonResult.put("result",SUCCESS);
			}else{
				jsonResult.put("result",MESSAGE);
				jsonResult.put("message","用户名或密码不正确");
			}
		}catch (Exception e) {
			e.printStackTrace();
			try{
				jsonResult.put("result",FAIL);
			}catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		response.getWriter().print(jsonResult.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
